package designpattern.observer.p3;

import io.reactivex.rxjava3.subjects.PublishSubject;
import io.reactivex.rxjava3.subjects.Subject;

sealed interface CalcEvent permits CalcEvent.Step, CalcEvent.Complete {
    record Step(int step) implements CalcEvent {
        @Override
        public final String toString() {
            return "Step{ step=" + step +'}';
        }
    }

    record Complete() implements CalcEvent {
        @Override
        public final String toString() {
            return "Complete{}";
        }
    }
}

public class Calculator {

    private Subject<CalcEvent> subject = PublishSubject.create();

    public Subject<CalcEvent> getSubject() {
        return subject;
    }

    float calc() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            // perform complex calculation
            Thread.sleep(1000);
            subject.onNext(new CalcEvent.Step(i));

        }
        subject.onNext(new CalcEvent.Complete());
        return 10;
    }


}
